/**
 * Created by jimmy on 2017/6/9.
 */

var mysqlConn = require("./BaseDao");


exports.insWxUser = function (user,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("replace into wx_user set ?",user,function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
};

exports.insUser = function (user,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("replace into base_user set ?",user,function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
};


exports.getBaseUser = function (openId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select a.open_id,a.mobile,a.birthday,a.real_name,5 as level,50 as discount from wx_user a where a.open_id = ?",[openId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
};

exports.getWxUser = function (openId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select * from wx_user where open_id = ?",[openId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getUserAddr = function (userId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select * from user_addr where user_id = ? and isdefault = 1",[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getUserAddrById = function (userId,addrId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select * from user_addr where user_id = ? and addr_id = ?",[userId,addrId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}